﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skewworks.Win.Motif.Glide
{
    public partial class GlideDataGrid : UserControl, IEmbeddedControl
    {

        #region Variables

        private ISettings _settings;

        private GlideFonts _font;
        private int _rowCount, _rowHeight;

        #endregion

        public GlideDataGrid()
        {
            InitializeComponent();
            _settings = new GlideDataGridSettings(this);
            Width = 100;
            Height = 90;
            _font = GlideFonts.Droid_12;
            this.Font = GlideFont.DroidSans(12);
            RowCount = 3;
            RowHeight = 30;
            Draggable = true;
            TappableCells = true;
            SortableHeaders = true;
            ShowHeaders = true;
            ShowScrollbar = true;
            ScrollbarWidth = 4;
            HeadersBackColor = Color.FromArgb(112, 116, 118);
            HeadersFontColor = Color.White;
            ItemsBackColor = Color.White;
            ItemsAltBackColor = Color.FromArgb(224, 224, 224);
            ItemsFontColor = Color.Black;
            SelectedItemBackColor = Color.FromArgb(255, 242, 153);
            SelectedItemFontColor = Color.Black;
            GridColor = Color.FromArgb(191, 197, 201);
            ScrollbarBackColor = Color.FromArgb(192, 192, 192);
            ScrollbarScrubberColor = Color.Black;
        }

        #region Properties

        public bool Draggable
        { get; set; }

        public GlideFonts GFont
        {
            get { return _font; }
            set
            {
                _font = value;
                this.Font = GlideFont.DroidSans((int)value);
                Invalidate();
            }
        }

        public Color GridColor
        { get; set; }

        public Color HeadersBackColor
        { get; set; }

        public Color HeadersFontColor
        { get; set; }

        public Color ItemsAltBackColor
        { get; set; }

        public Color ItemsBackColor
        { get; set; }

        public Color ItemsFontColor
        { get; set; }

        public int RowCount
        {
            get { return _rowCount; }
            set
            {
                _rowCount = value;
                Height = _rowCount * _rowHeight;
            }
        }

        public int RowHeight
        {
            get { return _rowHeight; }
            set
            {
                _rowHeight = value;
                Height = _rowCount * _rowHeight;
            }
        }

        public Color SelectedItemBackColor
        { get; set; }

        public Color SelectedItemFontColor
        { get; set; }

        public ISettings Settings
        {
            get { return _settings; }
        }

        public Color ScrollbarBackColor
        { get; set; }

        public Color ScrollbarScrubberColor
        { get; set; }

        public int ScrollbarWidth
        { get; set; }

        public bool ShowHeaders
        { get; set; }

        public bool ShowScrollbar
        { get; set; }

        public bool SortableHeaders
        { get; set; }

        public bool TappableCells
        { get; set; }

        #endregion

        #region GUI

        protected override void OnPaint(PaintEventArgs e)
        {
            lock (this)
            {
                int y = 0;
                Brush bsh1 = new SolidBrush(ItemsBackColor);
                Brush bsh2 = new SolidBrush(ItemsAltBackColor);
                Brush bCur = bsh2;

                // Draw Header
                e.Graphics.FillRectangle(new SolidBrush(HeadersBackColor), new Rectangle(0, 0, Width, RowHeight));
                y += RowHeight;

                while (y < Height)
                {
                    e.Graphics.FillRectangle(bCur, new Rectangle(0, y, Width, RowHeight));


                    y += RowHeight;

                    if (bCur == bsh1)
                        bCur = bsh2;
                    else
                        bCur = bsh1;
                }

                if (ScrollbarWidth > 0)
                    e.Graphics.FillRectangle(new SolidBrush(ScrollbarBackColor), new Rectangle(Width - ScrollbarWidth, 0, ScrollbarWidth, Height));
                if (Focused)
                {
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(0, 0, Width - 1, Height - 1));
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(1, 1, Width - 3, Height - 3));
                }
            }
        }

        #endregion

        #region Moving/Sizing

        private bool _sizeH, _move;
        private Point _sizeRef;

        private void GlideDataGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (_sizeH)
            {
                this.Width += e.X - _sizeRef.X;
                Invalidate();
            }
            else if (_move)
            {
                this.Left += e.X - _sizeRef.X;
                this.Top += e.Y - _sizeRef.Y;
                return;
            }
            else
            {
                if (e.X >= this.Width - 8)
                    this.Cursor = Cursors.SizeWE;
                else
                    this.Cursor = Cursors.Default;
            }
            _sizeRef = new Point(e.X, e.Y);
        }

        private void GlideDataGrid_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= this.Width - 8)
                    _sizeH = true;
            else
            {
                _move = true;
                this.Cursor = Cursors.SizeAll;
            }
        }

        private void GlideDataGrid_MouseUp(object sender, MouseEventArgs e)
        {
            _sizeH = false;
            _move = false;
            this.Cursor = Cursors.Default;
        }

        #endregion

    }
}
